﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using PowerSong.SongDatabase;
    using System;

    /// <summary>
    /// Interaction logic for EditNoticeWindow.xaml
    /// </summary>
    public partial class EditNoticeWindow : Window
        {

        public NoticeServiceItem Notice { get; private set; }

        private bool editing = false;

        public EditNoticeWindow(NoticeServiceItem existingNotice = null)
            {
            InitializeComponent();
            this.Notice = existingNotice;
            this.editing = existingNotice != null;
            if (existingNotice != null)
                {
                this.NameTextBox.Text = existingNotice.Notice.Title;
                this.VersesTextBox.Text = Utilities.VersesToText(existingNotice.Notice.Verses);
                }
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a title has been specified
                if (string.IsNullOrEmpty(NameTextBox.Text))
                    {
                    throw new Exception("Please specify a title for this notice so that it can be easily identified in the play list.");
                    }

                // Ensure some "verses" have been given
                if (string.IsNullOrEmpty(VersesTextBox.Text.Trim()))
                    {
                    throw new Exception("Please supply some notice text.");
                    }

                // Set title
                if (!editing) this.Notice = new NoticeServiceItem(new Song(), State.Settings.DefaultStyle);
                Notice.Notice.Title = NameTextBox.Text;
                
                // Put verses together
                Notice.Notice.Verses.Clear();
                Notice.Notice.Verses.AddRange(Utilities.TextToVerses(VersesTextBox.Text));

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Cannot Change Notice Details", ex.Message);
                }

            }

        }

    }
